/**
 * Created by liuzengrong on 2019/8/16.
 */
import React from 'react';
import { connect } from 'dva';
import StandardModule from '@/components/hyperspace/modules/StandardModule';
import PolicyInsuranceForm from './components/PolicyInsuranceForm';


// @ts-ignore
/**
 * 自定义模块
 */
// @ts-ignore
const LocalModule: React.FC = ({ insurance, dispatch }) => (
  <div>
    {insurance.modalVisible ?
      <PolicyInsuranceForm
        data={null}
        disabled={false}>
      </PolicyInsuranceForm>
      :
      <StandardModule
        data={insurance.data}
        dictionary={[
          {
            name: 'type',
            desc: '保险种类',
            dataType: 'string',
          },
          {
            name: 'insured',
            desc: '客户名称',
            dataType: 'string',
          },
          {
            name: 'insuredAmount',
            desc: '保险金额',
            dataType: 'string',
          },
          {
            name: 'insuranceCharge',
            desc: '保险费',
            dataType: 'string',
          },
        ]}
        config={{
          common: {
            formItemOptions: {
              type: {
                rules: [{ required: true, message: '该字段不能为空！' }],
              },
              insured: {
                rules: [{ required: true, message: '该字段不能为空！' }],
              },
              contactor: {
                rules: [{ required: true, message: '该字段不能为空！' }],
              },
              mobile: {
                rules: [{ required: true, message: '该字段不能为空！' }],
              },
            },
          },
          search: {
            fields: ['type', 'insured', 'contactor'], // 查询条件
            tabs: {
              field: 'status',
              tabItems: [
                {
                  title: '全部',
                  value: '',
                  active: true,
                },
                {
                  title: '待付款',
                  value: '1',
                },
                {
                  title: '待跟进',
                  value: '2',
                },
                {
                  title: '待投保',
                  value: '3',
                },
                {
                  title: '已完成',
                  value: '4',
                },
              ],
            }, // 状态栏
          },
          list: {
            url: '/api/insurance/query',
            fields: [
              'id',
              'type',
              'insured',
              'insuredAmount',
              'insuranceCharge',
              'contactor',
              'email',
              'mobile',
              'status',
              'creator',
            ], // 列表字段
            format: {
              username: {
                render: (value: any) => `格式化 - ${value}`,
              },
            },
            extensions: {
              operations: [
                (record: any) => (
                  <a
                    onClick={() => {
                      dispatch({
                        type: 'insurance/refreshState',
                        payload: {
                          modalVisible: true,
                        },
                      });
                    }}
                  >
                    跟单
                  </a>
                ),
              ],
            },
          },
          add: {
            url: '/api/insurance/create',
            // width: 680, // 非必填，默认680px
            // title: '新建表单', // 非必填，默认`新建`
            fields: [
              'type',
              'insured',
              'contactor',
              'mobile',
              'email',
            ], // 新增表单字段
          },
          delete: {
            url: '/api/insurance/remove',
            batchDeleteUrl: '', // 批量删除URL
            batchDelete: false, // 是否开启批量删除
            fields: ['id'], // 删除依据字段
          },
        }}
        namespace="insurance"
        dispatch={dispatch}
      />}
  </div>
);

// @ts-ignore
export default connect(({ insurance }) => ({
  insurance,
}))(LocalModule);
